package com.wenhuitec.productlib.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gzwh.weblib.bean.NzProductCategory;
import com.wenhuitec.productlib.R;
import com.wenhuitec.sysui.recyclerviewHelper.IItemTouchHelperAdapter;
import com.wenhuitec.sysui.recyclerviewHelper.OnStartDragListener;

import java.util.Collections;
import java.util.List;

public class CategorySortAdapter extends RecyclerView.Adapter<CategorySortAdapter.ViewHolder> implements IItemTouchHelperAdapter {
    private Context context;
    private List<NzProductCategory> list;
    private OnClickSwitchListener mOnClickSwitchListener;
    private final OnStartDragListener mDragListener;

    public CategorySortAdapter(Context context, List<NzProductCategory> list, OnStartDragListener mDragListener) {
        this.context = context;
        this.list = list;
        this.mDragListener = mDragListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_category, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        String str = list.get(i).getCategoryName();
        viewHolder.cateNameTv.setText(str);
//        viewHolder.foodNumTv.setText(list.get(i).getCounts() + "个商品");
        viewHolder.menu.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction()
                        == MotionEvent.ACTION_DOWN) {
                    mDragListener.onStartDrag(viewHolder);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(list, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onItemDismiss(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView cateNameTv;
        private ImageView menu;
        private TextView foodNumTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cateNameTv = itemView.findViewById(R.id.cateNameTv);
            menu = itemView.findViewById(R.id.item_list_menu_imageView);
            foodNumTv = itemView.findViewById(R.id.foodNumTv);
        }
    }

    public void setOnClickSwitchListener(OnClickSwitchListener onClickSwitchListener) {
        mOnClickSwitchListener = onClickSwitchListener;
    }

    public interface OnClickSwitchListener {
        void onClick(int position, boolean isChecked);
    }

    public List<NzProductCategory> getList() {
        return list;
    }
}